using System;
using System.Collections.Generic;
using System.Text;
using Musicman.Entities;

namespace Musicman.Entities.BusinessEntities
{
    public class SearchFileCollection : IEnumerable<Searchfile>, ICollection<Searchfile>
    {
        private List<Searchfile> _innerList = new List<Searchfile>();
        private Dictionary<String, Searchfile> _innerDictionary = new Dictionary<String, Searchfile>();
        object locker = new object();

        public List<Searchfile> InnerList
        {
            get { return _innerList; }
        }

        public static String GetIdentifier(Searchfile file)
        {
            return String.Format("{0}_{1}", file.Folder.Name.ToLower(), file.Name.ToLower());
        }


        #region IEnumerable<Searchfile> Members

        public IEnumerator<Searchfile> GetEnumerator()
        {
            return this.InnerList.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        #region ICollection<Searchfile> Members

        public void Add(Searchfile item)
        {
            lock (locker)
            {
                if (!Contains(item))
                {
                    string key = GetIdentifier(item);
                    this._innerList.Add(item);
                    this._innerDictionary.Add(key, item);
                }
            }
        }

        public void Clear()
        {
            this._innerList.Clear();
            this._innerDictionary.Clear();
        }

        public bool Contains(Searchfile item)
        {
            string key = GetIdentifier(item);
            return _innerDictionary.ContainsKey(key);
        }

        public void CopyTo(Searchfile[] array, int arrayIndex)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public int Count
        {
            get { return this._innerList.Count; }
        }

        public bool IsReadOnly
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public bool Remove(Searchfile item)
        {
            string key = GetIdentifier(item);
            if (_innerDictionary.ContainsKey(key))
            {
                Searchfile file = _innerDictionary[key];
                _innerList.Remove(file);
                _innerDictionary.Remove(key);
                return true;
            }
            return false;
        }

        #endregion


        public Searchfile this[int index]
        {
            get
            {
                return this._innerList[index];
            }
        }

        public Searchfile this[string key]
        {
            get
            {
                return this._innerDictionary[key];
            }
        }


    }
}
